package com.lakeworks.service.impl;

import com.lakeworks.common.core.text.Convert;
import com.lakeworks.domain.SysAttachment;
import com.lakeworks.mapper.SysAttachmentMapper;
import com.lakeworks.service.ISysAttachmentService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysAttachmentServiceImpl implements ISysAttachmentService {

    @Resource
    private SysAttachmentMapper sysAttachmentMapper;

    @Override
    @Cacheable(value = "attachment", key = "#id",unless = "#result == null")
    public SysAttachment selectSysAttachmentById(Long id) {
        return sysAttachmentMapper.selectSysAttachmentById(id);
    }


    @Override
    @Cacheable(value = "sysAttachments", key = "#root.methodName",unless = "#result?.size() == 0")
    public List<SysAttachment> attachmentList() {
        return sysAttachmentMapper.selectSysAttachmentList(new SysAttachment());
    }

    @Override
    @CacheEvict(value = "sysAttachments", key = "'attachmentList'")
    public int insertSysAttachment(SysAttachment sysAttachment) {
        return sysAttachmentMapper.insertSysAttachment(sysAttachment);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "sysAttachments", key = "'attachmentList'"),
            @CacheEvict(value = "attachment", key = "#sysAttachment.id")
    })
    public int updateSysAttachment(SysAttachment sysAttachment) {
        return sysAttachmentMapper.updateSysAttachment(sysAttachment);
    }

    @Override
    @CacheEvict(value = "sysAttachments", key = "'attachmentList'")
    public int deleteSysAttachmentByIds(String ids) {
        return sysAttachmentMapper.deleteSysAttachmentByIds(Convert.toStrArray(ids));
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "sysAttachments", key = "'attachmentList'"),
            @CacheEvict(value = "attachment", key = "#id")
    })
    public int deleteSysAttachmentById(Long id) {
        return sysAttachmentMapper.deleteSysAttachmentById(id);
    }

}
